package com.aaa.service.impl;

import com.aaa.dao.MemberListDao;


import com.aaa.entity.TbMember;
import com.aaa.service.MemberListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.vo.Result;


import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MemberListServiceImpl implements MemberListService {
    @Resource
    private MemberListDao memberListDao;

    @Override
    public Result<IPage<TbMember>> queryMemberList(Integer current, Integer pageSize, TbMember tbMember) {
            IPage<TbMember> page = new Page(current, pageSize);
            QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
            IPage<TbMember> iPage = memberListDao.selectPage(page, wrapper);
              List<TbMember> records = iPage.getRecords();
        if (StringUtils.hasText(tbMember.getMobile())) {
            String id = tbMember.getMobile();
            records = records.stream().filter(index -> index.getMobile().contains(id)).collect(Collectors.toList());
        }
        if(StringUtils.hasText(tbMember.getNickname())){
            String name = tbMember.getNickname();
            records=records.stream().filter(index -> index.getNickname().contains(name) ).collect(Collectors.toList());
            System.out.println(records.toString());
        }
        if (tbMember.getIsDisabled() != null){
            Integer isDisabled = tbMember.getIsDisabled();
            records=records.stream().filter(index -> index.getIsDisabled()==isDisabled ).collect(Collectors.toList());
        }

        if (tbMember.getGmtCreates() !=null ){
            if (tbMember.getGmtCreates() !=null && tbMember.getGmtCreates().size() > 0){
                String gmtCreate = tbMember.getGmtCreates().get(0);
                String gmt_modified = tbMember.getGmtCreates().get(1);
                // 转换时间格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                // 让String转换时间类型
                LocalDateTime parsedDate = LocalDateTime.parse(gmtCreate, formatter);
                LocalDateTime parsedDate1 = LocalDateTime.parse(gmt_modified, formatter);

                records=records.stream().filter(index -> index.getGmtCreate().isAfter(parsedDate) && index.getGmtCreate().isBefore(parsedDate1) ).collect(Collectors.toList());

            }
        }

            iPage.setRecords(records);
            return new Result<>(2000, "查询成功", iPage);
        }

    @Override
    public Result<String> updateStatus(Boolean isdisabled, String id) {
        TbMember member = new TbMember();
        if (isdisabled == true){
            member.setIsDisabled(1);
        }else{
            member.setIsDisabled(0);
        }

        UpdateWrapper<TbMember> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        int i = memberListDao.update(member,wrapper);
        if (i>0){
            return new Result(2000,"更新状态成功");
        }
        return null;
    }

    /**
     * 修改信息
     * @param tbMember
     * @return
     */
    @Override
    public Result updateMember(TbMember tbMember) {

        UpdateWrapper<TbMember> wrapper = new UpdateWrapper<>();
        wrapper.eq("id" , tbMember.getId());
        wrapper.set("nickname",tbMember.getNickname());
        wrapper.set("mobile",tbMember.getMobile());
        wrapper.set("sex",tbMember.getSex());
        wrapper.set("remark",tbMember.getRemark());
        memberListDao.update(null,wrapper);
        return new Result(2000,"修改成功");
    }


}
